package com.strato.file.remote.v1_0_0.server.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.strato.annotation.osgi.v1_0_0.service.OsgiWired;
import com.strato.file.remote.v1_0_0.server.AccessDeniedException;
import com.strato.file.remote.v1_0_0.server.FileServer;
import com.strato.logging.v1_0_0.api.Log;
import com.strato.mvc.annotation.v1_0_0.annotation.method.PathVariable;
import com.strato.mvc.annotation.v1_0_0.annotation.method.RequestMapping;
import com.strato.mvc.annotation.v1_0_0.annotation.type.Controller;

@Controller
@com.strato.mvc.annotation.v1_0_0.annotation.type.RequestMapping(FileServerController.WEB_PATH)
public class FileServerController {
	
	public static final String WEB_PATH="/strato.file.remote";

	@OsgiWired
	private Log log;
	
	@RequestMapping("/{serverId}/put")
	public void put(@PathVariable String serverId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String path=request.getParameter("path");
		FileServer server = FileServer.getServer(serverId);
		if(server==null){
			throw new NullPointerException("Server "+serverId+" not found");
		}
		String password=extractPassword(request);
		try{
			server.put(path, request.getInputStream(),password);
		}catch(AccessDeniedException e){
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
		}catch(IOException e){
			log.error("",e);
			throw e;
		}catch(RuntimeException e){
			log.error("",e);
			throw e;
		}
	}
	
	private String extractPassword(HttpServletRequest request) {
		return request.getHeader(FileServer.PASSWORD_HEADER);
	}

	@RequestMapping("/{serverId}/get")
	public void get(@PathVariable String serverId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String path=request.getParameter("path");
		FileServer server = FileServer.getServer(serverId);
		if(server==null){
			throw new NullPointerException("Server "+serverId+" not found");
		}
		String password=extractPassword(request);
		try{
			if(server.exists(path,password)){
				server.get(path, response.getOutputStream(),password);
				return;
			}
		}catch(AccessDeniedException e){
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
			return;
		}
		response.sendError(404,"STRATO FILE NOT FOUND");
	}
	
	@RequestMapping("/{serverId}/delete")
	public void delete(@PathVariable String serverId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String path=request.getParameter("path");
		FileServer server = FileServer.getServer(serverId);
		if(server==null){
			throw new NullPointerException("Server "+serverId+" not found");
		}
		String password=extractPassword(request);
		try{
			server.delete(path,password);
		}catch(AccessDeniedException e){
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
		}
	}

	@RequestMapping("/{serverId}/exists")
	public void exists(@PathVariable String serverId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String path=request.getParameter("path");
		FileServer server = FileServer.getServer(serverId);
		if(server==null){
			throw new NullPointerException("Server "+serverId+" not found");
		}
		String password=extractPassword(request);
		boolean exists=false;
		try{
			exists=server.exists(path,password);
		}catch(AccessDeniedException e){
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
			return;
		}
		PrintWriter out = response.getWriter();
		out.write(""+exists);
		out.flush();
	}

	@RequestMapping("/{serverId}/copy")
	public void copy(@PathVariable String serverId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String from=request.getParameter("from");
		String to=request.getParameter("to");
		String password=extractPassword(request);
		FileServer server = FileServer.getServer(serverId);
		if(server==null){
			throw new NullPointerException("Server "+serverId+" not found");
		}
		try{
			server.copy(from, to,password);
		}catch(AccessDeniedException e){
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
		}
	}
}
